package cn.sumpu.app.coolsk.dao.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import cn.sumpu.app.coolsk.dao.TicketDao;
import cn.sumpu.app.coolsk.domain.Ticket;
import cn.sumpu.app.coolsk.domain.TicketPrice;
import cn.sumpu.app.coolsk.exception.AppBusinessException;

public class TicketDaoImpl extends SqlSessionDaoSupport implements TicketDao {

	private static Logger logger = LoggerFactory.getLogger(TicketDaoImpl.class);

	

	@SuppressWarnings("unchecked")
	@Override
	public List<Ticket> getTicket(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList("cn.sumpu.app.coolsk.persistence.TicketMapper.getTicket",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Query database failure.", e);
			}
			throw new AppBusinessException("Query database failure.", e);
		}
	}
	
	

	@Override
	public int getTicketCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = (Integer) getSqlSession()
					.selectOne("cn.sumpu.app.coolsk.persistence.TicketMapper.getTicketCount",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Query database failure.", e);
			}
			throw new AppBusinessException("Query database failure.", e);
		}
	}

	@Override
	public void insertTicket(Ticket ticket) throws AppBusinessException {
		try {
			getSqlSession().insert("cn.sumpu.app.coolsk.persistence.TicketMapper.insertTicket",
							ticket);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
		
	}

	@Override
	public void updateTicket(Ticket ticket) throws AppBusinessException {
		try {
			getSqlSession().update("cn.sumpu.app.coolsk.persistence.TicketMapper.updateTicket",
							ticket);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
		
	}

	@Override
	public void delTicket(Map<String, Object> map) throws AppBusinessException {
		try {
			getSqlSession().delete("cn.sumpu.app.coolsk.persistence.TicketMapper.delTicket",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TicketPrice> getTicketPrice(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList("cn.sumpu.app.coolsk.persistence.TicketMapper.getTicketPrice",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Query database failure.", e);
			}
			throw new AppBusinessException("Query database failure.", e);
		}
	}

	@Override
	public void delTicketPrice(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession().delete("cn.sumpu.app.coolsk.persistence.TicketMapper.delTicketPrice",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("delete database failure.", e);
			}
			throw new AppBusinessException("delete database failure.", e);
		}
	}

	@Override
	public void insertTicketPrice(TicketPrice tp) throws AppBusinessException {
		try {
			getSqlSession().insert("cn.sumpu.app.coolsk.persistence.TicketMapper.insertTicketPrice",tp);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("delete database failure.", e);
			}
			throw new AppBusinessException("insert database failure.", e);
		}
	}

	

}
